package com.example.choupaiapp.main.activity.home;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.DateFormatUtil;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.activity.publish.PublishNewActivity;
import com.example.choupaiapp.main.bean.CameraDataBean;
import com.otaliastudios.cameraview.CameraUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

import butterknife.OnClick;
import uk.co.senab.photoview.PhotoView;


public class PicturePreviewActivity extends CommonActivity {

    private static WeakReference<byte[]> image;
    private byte[] b;
    private String flag;

    public static void setImage(@Nullable byte[] im) {
        image = im != null ? new WeakReference<>(im) : null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_preview);
    }

    @Override
    protected void initView() {
        final PhotoView imageView = findViewById(R.id.image);

        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int screenWidth = metric.widthPixels;
        int screenHeight = metric.heightPixels;

        //final int nativeWidth = getIntent().getIntExtra("nativeWidth", 0);
        //final int nativeHeight = getIntent().getIntExtra("nativeHeight", 0);
        flag = getIntent().getStringExtra("flag");
        b = image == null ? null : image.get();
        if (b == null) {
            finish();
            return;
        }

        CameraUtils.decodeBitmap(b, screenWidth, screenHeight, new CameraUtils.BitmapCallback() {
            @Override
            public void onBitmapReady(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);

                //比例
//                AspectRatio nativeRatio = AspectRatio.of(nativeWidth, nativeHeight);

            }
        });
    }

    @Override
    protected void initData() {

    }


    @OnClick(R.id.complete)
    public void onViewClicked() {
        if (Utils.isDoubleClick()) {
            return;
        }
        showDialog(context, "保存中...");
        //File savedPhoto = new File(Environment.getExternalStorageDirectory(), "cp_photo" + System.currentTimeMillis() + ".jpg");
        File savedPhoto = new File(Utils.getPickerFileDirectory(context, "cp_photo"), System.currentTimeMillis() + ".jpg");
        try {
            FileOutputStream outputStream = new FileOutputStream(savedPhoto.getPath());
            outputStream.write(b);
            outputStream.flush();
            outputStream.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

        dismissDialog();
        ArrayList<String> list = new ArrayList<>();
        list.add(savedPhoto.getAbsolutePath());

        if ("out".equals(flag)) {
            EventBus.getDefault().post("finish_camera");
            Bundle bundle = new Bundle();
            bundle.putString("flag", "no_text");
            bundle.putString("isVideo", "no");
            bundle.putStringArrayList("path", list);
            bundle.putString("shootTime", DateFormatUtil.ms4Date(System.currentTimeMillis()));
            startFinishActivity(PublishNewActivity.class, bundle);
        } else {
            EventBus.getDefault().post("finish_camera");
            CameraDataBean cameraDataBean = new CameraDataBean();
            cameraDataBean.setFlag("no_text");
            cameraDataBean.setTime(DateFormatUtil.ms4Date(System.currentTimeMillis()));
            cameraDataBean.setIsVideo("no");
            cameraDataBean.setList(list);
            EventBus.getDefault().post(cameraDataBean);
            finishActivity();
        }
    }
}
